<?php	
class M_pre_applicant Extends CI_Model{

	public function register_complete_info($profile_data)
	{
		unset($profile_data['brombrom']);
		$profile_data = (object)$profile_data;
		
		//format date and get age from birhtdate
		$d = $this->__format($profile_data->child_bdate);
		
		$profile_input['p_a_grade_level'] = $profile_data->level_id;
		$profile_input['p_a_sy_from'] = $profile_data->sy_from;
		$profile_input['p_a_sy_to'] = $profile_data->sy_to;
		$profile_input['p_a_created_at'] = NOW;
		$profile_input['p_a_dob'] = $d->dob;
		$profile_input['p_a_mobile_phone'] = $profile_data->mobile_phone;
		$profile_input['p_a_ay_id'] = $profile_data->ay_id;
		$profile_input['p_a_age'] = $d->age;
		$profile_input['p_a_schedule_for_testing'] = $profile_data->schedule_for_testing;
		$profile_input['p_a_firstname'] = $profile_data->child_fname;
		$profile_input['p_a_lastname'] = $profile_data->child_lname;
		$profile_input['p_a_middlename'] = strtolower($profile_data->child_mname) == 'none' ? NULL : $profile_data->child_mname;
		$profile_input['p_a_hear_from'] = $profile_data->hear_from;
		$profile_input['p_a_description'] = $profile_data->desc;
		$profile_input['p_a_price'] = $profile_data->price;
		
		$this->db->insert('pre_applicants',$profile_input);
		return $this->db->affected_rows() > 0 ? TRUE : FALSE;	
	}

	private function __format($date)
	{
		$date = DateTime::createFromFormat('m/d/Y', $date);
		$dob = $date->format('Y-m-d');
		$birthday = strtotime($dob);
		$time = time();
		$seconds = $time - $birthday;
		$age = floor($seconds / 31536000);
		return (object)array('dob'=>$dob,'age'=>$age);
	}
}